<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use fast\Random;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use think\Db;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        return $this->view->fetch();
    }

    public function exportUser()
    {
        exit;
        $filePath = ROOT_PATH . 'public/export/user.csv';
        $reader   = new Csv();
        // 设置编码读取csv文件
        $reader->setInputEncoding('GB2312');
        $spreadsheet = $reader->load($filePath);
        // 获取数据
        $sheet    = $spreadsheet->getActiveSheet();
        $rowCount = $sheet->getHighestRow();

        for ($row = 2; $row <= $rowCount + 1; $row++) {
            // 手机号码
            $mobile = trim($sheet->getCell('C' . $row)->getValue());

            if (empty($mobile)) {
                continue;
            }

            // 昵称
            $nickname = trim($sheet->getCell('B' . $row)->getValue());
            // 会员身份
            $userType = trim($sheet->getCell('H' . $row)->getValue());
            // 积分
            $integral = trim($sheet->getCell('L' . $row)->getValue());
            // 余额
            $money = trim($sheet->getCell('M' . $row)->getValue());
            // 累计消费
            $total = trim($sheet->getCell('N' . $row)->getValue());
            // 会员身份id
            $userGroupId = Db::name('user_group')->where(['name' => $userType])->value('id');

            $mobile     = str_replace('+', '', $mobile);
            $userId     = Db::name('user')->where(['mobile' => $mobile])->value('id');
            $userConfig = json_decode(\addons\shopro\model\Config::get(['name' => 'user'])->value, true);

            if (empty($userId)) {
                // 创建用户
                Db::name('user')->insertGetId([
                    'username'      => Random::alnum(20),
                    'nickname'      => empty($nickname) ? $userConfig['nickname'] : $nickname,
                    'mobile'        => $mobile,
                    'avatar'        => $userConfig['avatar'],
                    'group_id'      => empty($userGroupId) ? 1 : $userGroupId,
                    'money'         => $money,
                    'score'         => $integral,
                    'total_consume' => $total,
                    'createtime'    => time(),
                    'updatetime'    => time(),
                    'status'        => 'normal'
                ]);
            } else {
                Db::name('user')->update([
                    'id'            => $userId,
                    'group_id'      => empty($userGroupId) ? 1 : $userGroupId,
                    'money'         => $money,
                    'score'         => $integral,
                    'total_consume' => $total,
                    'createtime'    => time(),
                    'updatetime'    => time(),
                ]);
            }
        }

        echo '执行完毕';
    }

    public function exportGoods()
    {
        $filePath = ROOT_PATH . 'public/export/goods.csv';
        $reader   = new Csv();
        // 设置编码读取csv文件
        $reader->setInputEncoding('GB2312');
        $spreadsheet = $reader->load($filePath);
        // 获取数据
        $sheet    = $spreadsheet->getActiveSheet();
        $rowCount = $sheet->getHighestRow();
        for ($row = 2; $row <= $rowCount + 1; $row++) {
            // 轮播图
            $imagesArr = $sheet->getCell('I' . $row)->getValue();
            $images    = '';
            if (!empty($imagesArr)) {
                $imagesArr = array_column(json_decode($imagesArr, true), 'pic_url');
                $images    = implode(',', $imagesArr);
            }

//            if ($sheet->getCell('B' . $row)->getValue()) {
//                $data = [
//                    'type'           => 'normal',
//                    'title'          => $sheet->getCell('B' . $row)->getValue(),
//                    'subtitle'       => $sheet->getCell('C' . $row)->getValue(),
//                    'status'         => 'up',
//                    'weigh'          => $sheet->getCell('AB' . $row)->getValue(),
//                    'category_ids'   => 34,
//                    'image'          => $sheet->getCell('H' . $row)->getValue(),
//                    'images'         => $images,
//                    'price'          => $sheet->getCell('L' . $row)->getValue(),
//                    'original_price' => $sheet->getCell('E' . $row)->getValue(),
//                    'is_sku'         => 0,
//                    'show_sales'     => 0,
//                    'createtime'     => time()
//                ];
//                Db::name('shopro_goods')->insert($data);
//            }
        }
    }

}
